﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Events.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import __AS3__.vec.*;
    import GameMode.*;
    import Modules.stats.experiments.*;
    import Engine.*;
    import Modules.stats.types.*;

    public class SettingsMenu extends Sprite {

        private var m_bg:DisplayObject;
        private var m_trayToggleBtn:OptionButton;
        private var m_graphicsToggleBtn:OptionButton;
        private var m_sfxToggleBtn:OptionButton;
        private var m_musicToggleBtn:OptionButton;
        private var m_zoomInBtn:SpriteButton;
        private var m_zoomOutBtn:SpriteButton;
        private var m_fullscreenBtn:SpriteButton;
        private var m_coreButtons:Vector.<GameSprite>;
        private var m_isEnabled:Boolean;
        private var m_tutorialButtons:Vector.<GameSprite>;

        public function SettingsMenu(){
            this.m_bg = (new EmbeddedArt.Options_holder() as DisplayObject);
            addChild(this.m_bg);
            this.m_trayToggleBtn = new OptionButton(EmbeddedArt.Options_settings_open_up, EmbeddedArt.Options_settings_open_over, EmbeddedArt.Options_settings_open_down, this.trayToggleBtnClickHandler);
            this.m_trayToggleBtn.toolTip = ZLoc.t("Main", "Settings_ToolTip");
            this.m_trayToggleBtn.x = 32;
            this.m_trayToggleBtn.y = 3;
            addChild(this.m_trayToggleBtn);
            this.m_coreButtons = new Vector.<GameSprite>();
            this.m_tutorialButtons = new Vector.<GameSprite>();
            this.m_zoomOutBtn = new SpriteButton(EmbeddedArt.FV_Main_ZoomOut_Norm, EmbeddedArt.FV_Main_ZoomOut_Over, EmbeddedArt.FV_Main_ZoomOut_Down, this.zoomOutBtnClickHandler);
            this.m_zoomOutBtn.toolTip = ZLoc.t("Main", "ZoomOut_ToolTip");
            this.m_coreButtons.push(this.m_zoomOutBtn);
            addChild(this.m_zoomOutBtn);
            this.m_zoomInBtn = new SpriteButton(EmbeddedArt.FV_Main_ZoomIn_Norm, EmbeddedArt.FV_Main_ZoomIn_Over, EmbeddedArt.FV_Main_ZoomIn_Down, this.zoomInBtnClickHandler);
            this.m_zoomInBtn.toolTip = ZLoc.t("Main", "ZoomIn_ToolTip");
            this.m_coreButtons.push(this.m_zoomInBtn);
            addChild(this.m_zoomInBtn);
            this.m_graphicsToggleBtn = new OptionButton(EmbeddedArt.FV_Main_Options_Visual_Normal, EmbeddedArt.FV_Main_Options_Visual_Over, EmbeddedArt.FV_Main_Options_Visual_Down, this.graphicsToggleBtnClickHandler);
            this.m_graphicsToggleBtn.toolTip = ZLoc.t("Main", "Graphics_ToolTip");
            this.m_coreButtons.push(this.m_graphicsToggleBtn);
            addChild(this.m_graphicsToggleBtn);
            this.m_fullscreenBtn = new SpriteButton(EmbeddedArt.FV_Main_Zoom_FullScreen_Norm, EmbeddedArt.FV_Main_Zoom_FullScreen_Over, EmbeddedArt.FV_Main_Zoom_FullScreen_Over, this.fullscreenBtnClickHandler);
            this.m_fullscreenBtn.toolTip = ZLoc.t("Main", "FullScreen_ToolTip");
            this.m_coreButtons.push(this.m_fullscreenBtn);
            addChild(this.m_fullscreenBtn);
            this.m_sfxToggleBtn = new OptionButton(EmbeddedArt.FV_Main_Options_SFX_Normal, EmbeddedArt.FV_Main_Options_SFX_Over, EmbeddedArt.FV_Main_Options_SFX_Down, this.sfxToggleBtnClickHandler);
            this.m_sfxToggleBtn.toolTip = ZLoc.t("Main", "SoundFX_ToolTip");
            this.m_coreButtons.push(this.m_sfxToggleBtn);
            this.m_tutorialButtons.push(this.m_sfxToggleBtn);
            addChild(this.m_sfxToggleBtn);
            this.m_musicToggleBtn = new OptionButton(EmbeddedArt.FV_Main_Options_Music_Normal, EmbeddedArt.FV_Main_Options_Music_Over, EmbeddedArt.FV_Main_Options_Music_Down, this.musicToggleBtnClickHandler);
            this.m_musicToggleBtn.toolTip = ZLoc.t("Main", "Music_ToolTip");
            this.m_coreButtons.push(this.m_musicToggleBtn);
            this.m_tutorialButtons.push(this.m_musicToggleBtn);
            addChild(this.m_musicToggleBtn);
            var _local1:Number = 34;
            var _local2:Number = 5;
            var _local3:Number = 26;
            var _local4:int;
            while (_local4 < this.m_coreButtons.length) {
                this.m_coreButtons[_local4].x = (((_local4 % 2) * _local3) + _local2);
                this.m_coreButtons[_local4].y = ((Math.floor((_local4 / 2)) * _local3) + _local1);
                _local4++;
            };
            Global.world.addEventListener(FarmGameWorldEvent.USER_CHANGED, this.userChangedHandler);
            this.isEnabled = false;
            this.updateView();
            addEventListener(Event.ADDED_TO_STAGE, this.addedToStageHandler, false, 0, true);
            addEventListener(MouseEvent.MOUSE_MOVE, this.mouseOverHandler, false, 0, true);
            addEventListener(MouseEvent.MOUSE_DOWN, this.mouseOverHandler, false, 0, true);
            addEventListener(MouseEvent.MOUSE_UP, this.mouseOverHandler, false, 0, true);
            addEventListener(MouseEvent.CLICK, this.mouseOverHandler, false, 0, true);
        }
        public static function handleFullscreenToggle():void{
            var _local1:Vector2;
            StatsManager.sample(100, StatsCounterType.HUD_COUNTER, StatsKingdomType.SETTINGS, "full_screen");
            Utilities.toggleFullScreen();
            if (Utilities.isFullScreen()){
                _local1 = GlobalEngine.viewport.getScrollPosition();
                _local1 = GameMode.limitBackgroundScrolling(_local1);
                GlobalEngine.viewport.setScrollPosition(_local1);
            };
            UI.displayNewFranchise(true);
            Global.world.updateWorldObjectCulling(GameWorld.CULL_CHECK_ALL);
        }

        public function get isEnabled():Boolean{
            return (this.m_isEnabled);
        }
        public function set isEnabled(_arg1:Boolean):void{
            var _local2:GameSprite;
            this.m_isEnabled = _arg1;
            for each (_local2 in this.m_coreButtons) {
                _local2.mouseEnabled = (_local2.mouseChildren = this.m_isEnabled);
            };
            if (this.m_isEnabled){
                this.m_trayToggleBtn.hoverImage = EmbeddedArt.Options_settings_open_over;
                this.m_trayToggleBtn.nonSelectedImage = EmbeddedArt.Options_settings_open_up;
                this.m_trayToggleBtn.selectedImage = EmbeddedArt.Options_settings_open_down;
                this.m_trayToggleBtn.updateImageState();
            } else {
                this.m_trayToggleBtn.hoverImage = EmbeddedArt.Options_settings_closed_over;
                this.m_trayToggleBtn.nonSelectedImage = EmbeddedArt.Options_settings_closed_up;
                this.m_trayToggleBtn.selectedImage = EmbeddedArt.Options_settings_closed_down;
                this.m_trayToggleBtn.updateImageState();
            };
        }
        private function setGraphics(_arg1:Boolean=false):void{
            Global.player.options["graphicsLowQuality"] = _arg1;
            if (_arg1){
                Global.stage.quality = StageQuality.LOW;
            } else {
                if (!Game.m_blitting){
                    Global.stage.quality = StageQuality.HIGH;
                };
            };
            this.updateView();
        }
        private function setSfx(_arg1:Boolean=false):void{
            Global.player.options["sfxDisabled"] = _arg1;
            Sounds.setSoundManagerSFXMute();
            if (((!(Global.player.options["sfxDisabled"])) && (Global.experimentManager.inPerfExperiment(ExperimentDefinitions.EXPERIMENT_OPTIMIZE_LOADTIME, ExperimentDefinitions.EXPERIMENT_OPTIMIZE_SFX_LOADING)))){
                Sounds.startPostloading();
            };
            Global.ui.dispatchEvent(new FarmGameWorldEvent(FarmGameWorldEvent.SFX_CHANGED));
            this.updateView();
        }
        private function setMusic(_arg1:Boolean=false):void{
            Global.player.options["musicDisabled"] = _arg1;
            Sounds.setSoundManagerMusicMute();
            this.updateView();
        }
        private function updateView():void{
            this.m_musicToggleBtn.selected = !(Global.player.options["musicDisabled"]);
            this.m_sfxToggleBtn.selected = !(Global.player.options["sfxDisabled"]);
            this.m_graphicsToggleBtn.selected = !(Global.player.options["graphicsLowQuality"]);
        }
        public function setInTransition():void{
            var _local2:GameSprite;
            var _local3:Point;
            Global.stage.removeEventListener(Event.ENTER_FRAME, this.stageEnterFrameHandler);
            var _local1:Vector.<GameSprite> = this.m_coreButtons.slice();
            _local1.push(this.m_trayToggleBtn);
            for each (_local2 in _local1) {
                if (_local2.parent == Global.stage){
                    _local3 = this.globalToLocal(new Point(_local2.x, _local2.y));
                    _local2.x = _local3.x;
                    _local2.y = _local3.y;
                    addChild(_local2);
                };
            };
        }
        public function setEndTransition():void{
            var _local1:Point;
            var _local2:GameSprite;
            if (this.m_trayToggleBtn.parent == this){
                _local1 = this.localToGlobal(new Point(this.m_trayToggleBtn.x, this.m_trayToggleBtn.y));
                this.m_trayToggleBtn.x = _local1.x;
                this.m_trayToggleBtn.y = _local1.y;
            };
            if (this.m_isEnabled){
                for each (_local2 in this.m_tutorialButtons) {
                    if (_local2.parent == this){
                        _local1 = this.localToGlobal(new Point(_local2.x, _local2.y));
                        _local2.x = _local1.x;
                        _local2.y = _local1.y;
                    };
                };
            };
            Global.stage.addEventListener(Event.ENTER_FRAME, this.stageEnterFrameHandler);
        }
        public function setEndPostTutorialTransition():void{
            this.setInTransition();
        }
        private function addedToStageHandler(_arg1:Event):void{
            removeEventListener(Event.ADDED_TO_STAGE, this.addedToStageHandler);
            this.setEndTransition();
        }
        private function mouseOverHandler(_arg1:MouseEvent):void{
            _arg1.stopImmediatePropagation();
            _arg1.stopPropagation();
        }
        private function stageEnterFrameHandler(_arg1:Event):void{
            var _local3:Point;
            var _local4:Point;
            var _local5:GameSprite;
            var _local2:Point = localToGlobal(new Point(32, 3));
            this.m_trayToggleBtn.x = _local2.x;
            this.m_trayToggleBtn.y = _local2.y;
            Global.stage.addChild(this.m_trayToggleBtn);
            if (this.m_isEnabled){
                _local3 = localToGlobal(new Point(5, 86));
                this.m_sfxToggleBtn.x = _local3.x;
                this.m_sfxToggleBtn.y = _local3.y;
                _local4 = localToGlobal(new Point(31, 86));
                this.m_musicToggleBtn.x = _local4.x;
                this.m_musicToggleBtn.y = _local4.y;
                for each (_local5 in this.m_tutorialButtons) {
                    Global.stage.addChild(_local5);
                };
            };
        }
        private function userChangedHandler(_arg1:FarmGameWorldEvent):void{
            this.updateView();
        }
        private function trayToggleBtnClickHandler(_arg1:Event):void{
            _arg1.stopImmediatePropagation();
            dispatchEvent(new UIEvent(UIEvent.SETTINGS_TOGGLE_CLICK, null));
        }
        private function graphicsToggleBtnClickHandler(_arg1:Event):void{
            _arg1.stopImmediatePropagation();
            this.setGraphics(!(this.m_graphicsToggleBtn.selected));
            StatsManager.sample(100, StatsCounterType.HUD_COUNTER, StatsKingdomType.SETTINGS, "graphics_quality");
            GameTransactionManager.addTransaction(new TSaveOptions(Global.player.options));
        }
        private function sfxToggleBtnClickHandler(_arg1:Event):void{
            _arg1.stopImmediatePropagation();
            this.setSfx(!(this.m_sfxToggleBtn.selected));
            StatsManager.sample(100, StatsCounterType.HUD_COUNTER, StatsKingdomType.SETTINGS, "sfx_on_off");
            GameTransactionManager.addTransaction(new TSaveOptions(Global.player.options));
        }
        private function musicToggleBtnClickHandler(_arg1:Event):void{
            _arg1.stopImmediatePropagation();
            this.setMusic(!(this.m_musicToggleBtn.selected));
            StatsManager.sample(100, StatsCounterType.HUD_COUNTER, StatsKingdomType.SETTINGS, "music_on_off");
            GameTransactionManager.addTransaction(new TSaveOptions(Global.player.options));
        }
        private function zoomInBtnClickHandler(_arg1:Event):void{
            var _local2:Number;
            _arg1.stopImmediatePropagation();
            if (Global.world.isZoomEnabled()){
                _local2 = GlobalEngine.viewport.getZoom();
                _local2 = (_local2 + Global.gameSettings.getNumber("zoomStep"));
                _local2 = Math.min(Global.gameSettings.getNumber("maxZoom"), _local2);
                _local2 = Math.max(Global.gameSettings.getNumber("minZoom"), _local2);
                StatsManager.sample(100, StatsCounterType.HUD_COUNTER, StatsKingdomType.SETTINGS, "zoom_in");
                if (GlobalEngine.viewport.getZoom() != _local2){
                    GlobalEngine.viewport.setZoom(_local2);
                    Global.world.updateWorldObjectCulling(GameWorld.CULL_ZOOM_IN);
                    CloudManager.updateZoomFactor(_local2);
                };
            };
        }
        private function zoomOutBtnClickHandler(_arg1:Event):void{
            var _local2:Number;
            var _local3:Vector2;
            _arg1.stopImmediatePropagation();
            if (Global.world.isZoomEnabled()){
                _local2 = GlobalEngine.viewport.getZoom();
                _local2 = (_local2 - Global.gameSettings.getNumber("zoomStep"));
                _local2 = Math.min(Global.gameSettings.getNumber("maxZoom"), _local2);
                _local2 = Math.max(Global.gameSettings.getNumber("minZoom"), _local2);
                StatsManager.sample(100, StatsCounterType.HUD_COUNTER, StatsKingdomType.SETTINGS, "zoom_out");
                if (GlobalEngine.viewport.getZoom() != _local2){
                    GlobalEngine.viewport.setZoom(_local2);
                    _local3 = GlobalEngine.viewport.getScrollPosition();
                    _local3 = GameMode.limitBackgroundScrolling(_local3);
                    GlobalEngine.viewport.setScrollPosition(_local3);
                    Global.world.updateWorldObjectCulling(GameWorld.CULL_ZOOM_OUT);
                    CloudManager.updateZoomFactor(_local2);
                };
            };
        }
        private function fullscreenBtnClickHandler(_arg1:Event):void{
            _arg1.stopImmediatePropagation();
            handleFullscreenToggle();
        }

    }
}//package Display 
